/*
 * @Descripttion: 
 * @version: 
 * @Author: TT
 * @Date: 2023-10-21 17:58:14
 * @LastEditors: TT-hzy
 * @LastEditTime: 2024-08-17 10:55:51
 */

import 'package:flutter/material.dart';

import 'package:hzy_project_template/base_ui_config/base_ui_config_index.dart';
import 'package:hzy_project_template/commpontents/common_long_images_widget.dart';
import 'package:hzy_project_template/commpontents/commpontents_index.dart';
import 'package:hzy_project_template/config/config_index.dart';
import 'package:hzy_project_template/models/models_index.dart';
import 'package:hzy_project_template/pages/tu_chong/models/tu_chong_model.dart';
import 'package:hzy_project_template/pages/tu_chong/tools/tu_chong_utils.dart';
import 'package:hzy_project_template/utils/utils_index.dart';

import '../vm/tu_chong_vm.dart';
import '../widgets/example_subscript_widget.dart';

class TuChongDetailV extends CommonGetXV<TuChongDetailC> {
  TuChongDetailV({Key? key}) : super(key: key);
  @override
  TuChongDetailC get controller => Get.put(TuChongDetailC());

  @override
  Widget createBody({
    required BuildContext context,
    BoxConstraints? constraints,
  }) {
    Widget body = configLongImageWidget();
    body = Stack(
      children: [
        body,
        configBottomWidget(),
        configSubscriptWidget(),
      ],
    );
    return body;
  }

  /// 创建长图
  configLongImageWidget() {
    Widget body = CommonLongImageSWidget(
      key: controller.longKey,
      heroSub: controller.heroTag,
      imageModellist: controller.imagelist,
      scrollerIndex: (index) {
        controller.configChangeIndex(index: index);
      },
      index: controller.currentIndex,
      scrollStar: (notification) {
        controller.scrollEnd = false;
      },
      scrollUpdata: (notification) {
        controller.scrollEnd = false;
        controller.configScrollerOffset(offset: notification.metrics.pixels);
      },
      scrollEnd: (notification) {
        controller.scrollEnd = true;
      },
      userScroll: (notification) {
        controller.configAnimationChangeState(isStar: false);
      },
      tapImageOnLongPress: (index, imageNormalModel) {
        controller.configLongPress(index: index);
      },
      tapImageCall: (index, imageNormalModel) {
        controller.tapImage(imageNormalModel: imageNormalModel);
      },
      isNeedHero: false,
    );
    body = Center(
      child: body,
    );
    return body;
  }

  /// 创建角标
  configSubscriptWidget() {
    Widget body = ExampleSubscriptWidget(
      key: controller.subScriptkey,
      allNum: controller.imagelist.length,
      index: controller.currentIndex,
    );
    body = Positioned(
      right: 0,
      left: 0,
      top: 0,
      child: body,
    );
    return body;
  }

  /// 创建底部视图
  configBottomWidget() {
    Widget body = GetBuilder<TuChongDetailC>(
      builder: (controller) {
        Widget bt = createTuchongImageDetailBottomWidget();
        bt = SlideTransition(
          position: controller.animation,
          child: bt,
        );
        return bt;
      },
    );

    body = Positioned(
      bottom: (SizeMacro.bottomBarHeight == 0 ? 10 : SizeMacro.bottomBarHeight),
      right: 10,
      left: 10,
      child: body,
    );
    return body;
  }

// 创建图片详情底部模块
  createTuchongImageDetailBottomWidget() {
    Widget body = createBoxShaowBottomNavBarWidget(
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          IconButton(
            onPressed: () {
              controller.tapBack();
            },
            icon: const Icon(
              Icons.close,
              color: HzyNormalColorS.colffffff,
            ),
          ),
          Expanded(
            child: Text(
              controller.tuChongItem.title ?? "",
              style: HzyTextStyle.fontBold(
                size: 16,
                color: HzyNormalColorS.colffffff,
              ),
              maxLines: 1,
              overflow: TextOverflow.ellipsis,
              textAlign: TextAlign.center,
            ),
          ),
          IconButton(
            onPressed: () {
              controller.configLongPress(
                index: controller.currentIndex,
              );
            },
            icon: Padding(
              padding: const EdgeInsets.only(
                right: 3,
              ),
              child: Text(
                LaunchIdConfig.enjoyment.tr,
                style: HzyTextStyle.fontBold(
                  size: 14,
                  color: HzyNormalColorS.colffffff,
                ),
              ),
            ),
          )
        ],
      ),
      color: const Color(0xff201e22),
    );

    return body;
  }
}

class TuChongDetailC extends CommonGetXController
    with GetTickerProviderStateMixin {
  // --------- 公开属性  --------- //
  /// 转换后的数据源
  List<ImageNormalModel> imagelist = [];

  /// 当前下标
  int currentIndex = 0;

  /// 数据源
  late TuChongItem tuChongItem;

  /// 动画控制器
  late AnimationController animationController;

  /// 上一次滑动距离
  double oldOffset = 0;

  /// 底部距离
  double? bottom;

  /// 滚动结束
  bool scrollEnd = true;

  /// 底部
  Tween<Offset> bottomTween = Tween(
    begin: const Offset(0, 0),
    end: const Offset(0, 2),
  );

  /// 移动动画
  late Animation<Offset> animation;
  TuChongVM tuChongVM = TuChongVM();

  /// 下标key
  GlobalKey<ExampleSubscriptState> subScriptkey = GlobalKey();

  /// 长图key
  GlobalKey<CommonLongImageSState> longKey = GlobalKey();

  String heroTag = "hero";
  // --------- 生命周期  --------- //
  @override
  configDefault() {
    isShowAppBar = false;
    scallBackGroundColor = const Color(0xff181818);
    safeAreatop = false;

    /// 创建动画控制器
    animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 500),
    );

    animationController.addStatusListener((status) {});

    /// 创建动画解释器
    animation = bottomTween.animate(
      CurvedAnimation(parent: animationController, curve: Curves.linear),
    );

    var params = currentGetParams();
    if (params["index"] != null) {
      currentIndex = int.parse(params["index"] ?? "0");
    }

    if (params['heroTag'] != null) {
      heroTag = params['heroTag'] ?? "";
    }

    /// 界面传值
    tuChongItem = currentGetArguments();
    imagelist = TuChongUtils.configDetailImageModelList(
      tuChongItem: tuChongItem,
      width: SizeMacro.screenWidth,
    );
  }
  // --------- 数据处理  --------- //

  // --------- 触发事件  --------- //
  /// 长图滚动监听
  configScrollerOffset({required double offset}) {
    if (offset > oldOffset && offset > 100) {
      animationController.forward();
    } else if (offset < oldOffset || offset < 0) {
      animationController.reverse();
    }
    oldOffset = offset;
  }

  configAnimationChangeState({
    required bool isStar,
  }) {
    if (isStar) {
      /// 动画开始
      animationController.forward();
    } else if (isStar == false && scrollEnd) {
      /// 动画逆执行
      animationController.reverse();
    }
  }

  /// 切换下标
  configChangeIndex({
    required int index,
  }) {
    if (currentIndex != index) {
      currentIndex = index;
      subScriptkey.currentState?.configCurrentIndex(index: index);
    }
  }

  tapImage({
    required ImageNormalModel imageNormalModel,
  }) {}

  /// 点击返回
  tapBack() {
    longKey.currentState?.scrollController.jumpTo(0);
    currentGoback();
  }

  /// 配置长按图片触发事件
  configLongPress({
    required int index,
  }) {
    showToolTop(
      popType: imagelist.length == 1 ? 1 : 0,
      list: imagelist,
      tapCall: (index) {
        if (judegUserIsLogin()) {
          if (index != 0) {
            configTapSelectImageTool(
              type: index,
              list: imagelist,
            );
          } else {
            tuChongVM.configAddCollect(
              tuChongItem: tuChongItem,
            );
          }
        }
      },
    );
  }
}
